<?php

namespace app\model;

use think\Model;

class UserBlack extends Model
{
	// 设置当前模型对应的数据表
	protected $name = 'user_black';
	// 设置当前模型主键
	protected $pk = 'id';

	public function selectByList(array $fields, int $page, int $limit, string $startDate, string $endDate, int $uid, string $phone): array
	{
		$where = [];

		if (!empty($uid)) {
			$where[] = ['uid', '=', $uid];
		}

		if (!empty($phone)) {
			$where[] = ['phone', '=', $phone];
		}

		return $this::field($fields)->where($where)->whereBetween('create_time', [$startDate, $endDate])->page($page)->limit($limit)->select()->toArray();
	}

	public function selectByListCount(string $startDate, string $endDate, int $uid, string $phone): int
	{
		$where = [];

		if (!empty($uid)) {
			$where[] = ['uid', '=', $uid];
		}

		if (!empty($phone)) {
			$where[] = ['phone', '=', $phone];
		}

		return $this::where($where)->whereBetween('create_time', [$startDate, $endDate])->count();
	}

	public function selectFirstByUid(array $fields, int $uid)
	{
		$data = $this->field($fields)->where(['uid' => $uid])->find();

		return $data ? $data->toArray() : [];
	}

	public function deleteByUid(string $uid)
	{
		return $this->where(['uid' => $uid])->delete();
	}
}
